package pl.un4giv3n.mightvsmagic.admin.action;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import pl.un4giv3n.mightvsmagic.action.PersistenceAction;
import pl.un4giv3n.mightvsmagic.entity.User;

public class ListUsersAction extends PersistenceAction{

	private static final long serialVersionUID = 1L;
	private List<User> players;
	private List<User> admins;
	
	@SuppressWarnings("unchecked")
	@Override
	public String execute() {
		EntityManager em = getEntityManager();
		User user = getSessionUser(em);
		if(user == null){
			addActionError("Zaloguj się");
			em.close();
			return LOGIN;
		}
		Query playersQuery = em.createNamedQuery("find.all.users");
		players = playersQuery.getResultList();
		Query adminQuery = em.createNamedQuery("find.admins");
		admins = adminQuery.getResultList();
		return super.execute();
	}

	public List<User> getPlayers() {
		return players;
	}

	public List<User> getAdmins() {
		return admins;
	}
	
}
